<?php
namespace app\common\model;
use x\Xbase;
class Shop extends Xbase
{
    //是否登录
    public function isLogin(){
        $token = cookie('token');
        if($token){
            $sql = "select * from shop where token=?";
            $rs = $this->db->query($sql,[$token],1);
            return $rs ? $rs : [];
        }
        return [];
    }

    /**
     * 登录
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     */
  	public function login($username, $password){
        /* 获取用户数据 */
        $shop = $this->db->query('select * from shop where username=? or phone=?',[$username,$username],1);
        if (empty($shop) || (int)$shop['status'] !== 0) {
            $this->setError('用户不存在或被禁用','username');
            return false;
        }
        if($shop['password'] != com_password($password)){
            $this->setError('密码错误','password');
            return false;
        }
        $token = token($shop['id']);
        $ip = ip();
        $data = [
            'id' => $shop['id'],
            'last_login_ip' => $ip,
            'last_login_time' => time(),
            'login_times' => $shop['login_times']+1,
            'update_time' => time(),
        ];
        $data['token'] = $token;
        $this->db->update('shop',$data);
        m('Log','xcom')->record($shop['id'], '登录', 'shop');
        cookie('token',$token);
        return $token;
  	}
}
